/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_announce_proxy.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
int32_t SleAnnounceProxy::InnerTransact(
    uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
{
    auto remote = Remote();
    if (remote == nullptr) {
        SLE_LOGE("[InnerTransact] fail: get Remote fail code %{public}d", code);
        return OBJECT_NULL;
    }
    int32_t err = remote->SendRequest(code, data, reply, flags);
    if (err != NO_ERROR) {
        SLE_LOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
    }
    return err;
}

SleErrorCode SleAnnounceProxy::RegisterAnnounceCallback(const sptr<ISleAnnounceCallback> &callback)
{
    SLE_LOGD("RegisterAnnounceCallback In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleAnnounceProxy::GetDescriptor()),
        SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()),
        SLE_ERR_IPC_TRANS_FAILED, "write announce callback failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleAnnounceInterfaceCode::SLE_REGISTER_ANNOUNCE_CALLBACK, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleAnnounceProxy::UnregisterAnnounceCallback(const sptr<ISleAnnounceCallback> &callback)
{
    SLE_LOGD("UnregisterAnnounceCallback In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleAnnounceProxy::GetDescriptor()),
        SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteRemoteObject(callback->AsObject()),
        SLE_ERR_IPC_TRANS_FAILED, "write announce callback failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleAnnounceInterfaceCode::SLE_UNREGISTER_ANNOUNCE_CALLBACK, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleAnnounceProxy::CreateAnnounceId(uint8_t &announceId)
{
    SLE_LOGD("CreateAnnounceId In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleAnnounceProxy::GetDescriptor()),
        SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleAnnounceInterfaceCode::SLE_CREATE_ANNOUNCE_ID, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }

    int32_t exception = reply.ReadInt32();
    SLE_ASSERT_AND_RETURN_RET_LOG(exception == NO_ERROR, exception, "create announce id failed!(%d)", exception);
    announceId = reply.ReadUint8();
    return (SleErrorCode)exception;
}

SleErrorCode SleAnnounceProxy::RemoveAnnounceId(uint8_t announceId)
{
    SLE_LOGD("CreateAnnounceId In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleAnnounceProxy::GetDescriptor()),
        SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(announceId), SLE_ERR_IPC_TRANS_FAILED, "write announce id failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleAnnounceInterfaceCode::SLE_REMOVE_ANNOUNCE_ID, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleAnnounceProxy::StartAnnounce(uint8_t announceId, AnnounceData &datas, AnnounceParam &params)
{
    SLE_LOGD("CreateAnnounceId In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleAnnounceProxy::GetDescriptor()),
        SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(announceId), SLE_ERR_IPC_TRANS_FAILED, "write announce id failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteParcelable(&datas), SLE_ERR_IPC_TRANS_FAILED, "write announce data failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(
        data.WriteParcelable(&params), SLE_ERR_IPC_TRANS_FAILED, "write announce param failed!");
    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleAnnounceInterfaceCode::SLE_ANNOUNCE_ENABLE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleAnnounceProxy::StopAnnounce(uint8_t announceId)
{
    SLE_LOGD("StopAnnounce In");
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleAnnounceProxy::GetDescriptor()),
        SLE_ERR_IPC_TRANS_FAILED, "write interface token failed!");
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteUint8(announceId), SLE_ERR_IPC_TRANS_FAILED, "write announce id failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleAnnounceInterfaceCode::SLE_ANNOUNCE_DISABLE, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    return (SleErrorCode)reply.ReadInt32();
}

SleErrorCode SleAnnounceProxy::GetDefaultAnnounceParamConfig(SleAnnounceParam &announceConfig)
{
    MessageParcel data;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.WriteInterfaceToken(SleAnnounceProxy::GetDescriptor()), SLE_ERR_IPC_TRANS_FAILED,
        "write interface token failed!");

    MessageParcel reply;
    MessageOption option = {MessageOption::TF_SYNC};
    int32_t result = InnerTransact(SleAnnounceInterfaceCode::SLE_GET_DEFAULT_ANNOUNCE_CONFIG, option, data, reply);
    if (result != NO_ERROR) {
        SLE_LOGE("transact done fail, error code: %{public}d", result);
        return SLE_ERR_IPC_TRANS_FAILED;
    }
    int32_t exception = reply.ReadInt32();
    SLE_ASSERT_AND_RETURN_RET_LOG(exception == NO_ERROR, exception, "get default announce param failed!(%d)", exception);
    announceConfig.handle = reply.ReadUint8();
    announceConfig.mode = reply.ReadUint8();
    announceConfig.role = reply.ReadUint8();
    announceConfig.level = reply.ReadUint8();
    announceConfig.annonceIntervalMin = reply.ReadUint32();
    announceConfig.annonceIntervalMax = reply.ReadUint32();
    announceConfig.channelMap = reply.ReadUint8();
    announceConfig.txPower = reply.ReadInt8();
    announceConfig.connectIntervalMin = reply.ReadUint16();
    announceConfig.connectIntervalMax = reply.ReadUint16();
    announceConfig.connectLatency = reply.ReadUint16();
    announceConfig.connectTimeout = reply.ReadUint16();
    return (SleErrorCode)exception;
}
} // SleStandard
} // NearLink
} // OHOS